package com.hyc.myvideo;

import android.app.Application;
import android.content.Context;

import com.hyc.myvideo.data.api.model.UserInfo;
import com.hyc.myvideo.ui.service.AudioService;
import com.hyc.myvideo.utils.AppConfig;
import com.jakewharton.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;
import com.umeng.socialize.Config;

import java.io.File;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.Cache;
import okhttp3.OkHttpClient;


/**
 * Created by 123123 on 2017/5/27.
 */
public class AppContext extends Application {
    private static Picasso picasso;
    /**
     * 用户信息
     */
    private static UserInfo userInfo;
    private AudioService audioService;

    public AudioService getAudioService() {
        return audioService;
    }

    public void setAudioService(AudioService audioService) {
        this.audioService = audioService;
    }

    public UserInfo getUserInfo() {
        if (userInfo == null) userInfo = new UserInfo();
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }


    public static AppContext get(Context context) {
        return (AppContext) context.getApplicationContext();
    }

    public static Picasso getPicasso() {
        return picasso;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Config.DEBUG = true;
        new AppConfig(this);
        PicassoCongfig();
    }

    public void onLowMemory() {
        super.onLowMemory();
    }

    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
    }

    private void PicassoCongfig() {
        if (picasso == null) {
            Picasso.Builder picassoBuilder = new Picasso.Builder(this);
            picassoBuilder.downloader(new OkHttp3Downloader(getOkHttp3Client()));
            picassoBuilder.executor(Executors.newSingleThreadExecutor());
            picasso = picassoBuilder.build();
            //debug的标示
            picasso.setIndicatorsEnabled(BuildConfig.DEBUG);
        }
    }

    /***
     * OkHttp3 作为图片下载器
     * @return
     */
    private OkHttpClient getOkHttp3Client() {
        int CACHE_SIZE = 10 * 1024 * 1024;
        File cacheDir = new File(this.getCacheDir(), "img");
        Cache caches = new Cache(cacheDir, CACHE_SIZE);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(15, TimeUnit.SECONDS)//连接超时(单位:秒)
                .writeTimeout(20, TimeUnit.SECONDS)//写入超时(单位:秒)
                .readTimeout(20, TimeUnit.SECONDS)//读取超时(单位:秒)
                .cache(caches)//设置缓存
                .hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                })
                .build();
        return okHttpClient;
    }

}
